﻿using B.S.RbacData.Domain;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Read.API.Applications.Commands.RBAC;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.RbacData.Read.API.Applications.CommandHandlters.RBAC
{

    /// <summary>
    /// 编辑用户时反填角色处理
    /// </summary>
    public class GetUserIdCommandhandlter : IRequestHandler<GetUserIdCommand, ApiResult<List<int>>>
    {
        private readonly IBaseRepository<UserRoleModel> userRoleRep;

        public GetUserIdCommandhandlter(IBaseRepository<UserRoleModel> userRoleRep)
        {
            this.userRoleRep = userRoleRep;
        }


        public async Task<ApiResult<List<int>>> Handle(GetUserIdCommand request, CancellationToken cancellationToken)
        {


            var userrole = await userRoleRep.GetAll().Where(x => x.UserId == request.UserId).Select(x => x.RoleId).ToListAsync();

            return new ApiResult<List<int>>
            {
                code = ResultEnumCode.Ok,
                data = userrole,
                message = "查询成功"


            };


        }
    }
}
